<template>
	<el-dialog
			    v-model="dialogVisible_add"
			    title="Tips"
			    width="500"
			  >
			    <el-input type="text" v-model="name" placeholder="请输入添加的商品分类"></el-input>
			    <el-input type="text" v-model="img" placeholder="请输入添加的商品分类图片"></el-input>
				<!-- <el-select
				      v-model="roleValue"
				      placeholder="请选择角色"
				      size="large"
				      style="width: 240px"
				    >
				      <el-option
				        v-for="item in options"
				        :key="item.id"
				        :label="item.name"
				        :value="item.id"
				      />
				    </el-select> -->
			    <template #footer>
			      <div class="dialog-footer">
			        <el-button @click="dialogVisible_add = false">Cancel</el-button>
			        <el-button type="primary" @click="addSubmit">
			          Confirm
			        </el-button>
			      </div>
			    </template>
		</el-dialog>
	<el-dialog
		    v-model="dialogVisible_update"
		    title="Tips"
		    width="500"
		  >
		    <el-input type="text" v-model="updateName" placeholder="请输入想修改的商品分类"></el-input>
			<el-input type="text" v-model="img" placeholder="请输入添加的商品分类图片"></el-input>
		<!-- 	<el-select
			      v-model="roleValue"
			      placeholder="请选择角色"
			      size="large"
			      style="width: 240px"
			    >
			      <el-option
			        v-for="item in options"
			        :key="item.id"
			        :label="item.name"
			        :value="item.id"
			      />
			    </el-select> -->
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_update = false">Cancel</el-button>
		        <el-button type="primary" @click="updateSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
		  </el-dialog>
		  <el-dialog
		    v-model="dialogVisible_del"
		    title="Tips"
		    width="500"
		  >
		    确认要删除 {{delName}} 吗？
		    <template #footer>
		      <div class="dialog-footer">
		        <el-button @click="dialogVisible_del = false">Cancel</el-button>
		        <el-button type="primary" @click="delSubmit">
		          Confirm
		        </el-button>
		      </div>
		    </template>
		  </el-dialog>
	<el-table :data="tableData" style="width: 1200px;" row-key="_id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
	  <el-table-column prop="name" label="类型名" width="180" />
	  <el-table-column label="图片" width="180">
		  <template #default="scope">
			  <img :src="scope.row.img" style="width:100px;height:100px;" v-if="scope.row.img"/>
			  <div v-else>暂无</div>
		  </template>
	  </el-table-column>
	  <el-table-column prop="createTime" label="创建时间" />
	  <el-table-column prop="updateTime" label="更新时间" />
			<el-table-column>
				<template #header>操作</template>
				<template #default="scope">
				  <el-button type="primary" @click="add(scope.row._id)" v-if="scope.row.pid==0||scope.row.pid=='all'">添加</el-button>
				  <el-button type="warning" @click="update(scope.row._id,scope.row.name,scope.row.img)" v-if="scope.row.pid!='all'">编辑</el-button>
				  <el-button type="danger" @click="del(scope.row._id,scope.row.name)" v-if="scope.row.pid!='all'">删除</el-button>
				</template>
			</el-table-column>
	</el-table>
</template>

<script setup>
	import {ref} from 'vue'
	const tableData = ref([{_id:0,name:"商品分类",pid:"all",createTime:"2025-01-01 01:01:01",updateTime:"2025-01-01 01:01:01",children:[]}])
	import {http} from "../components/http.js"
	function query(){
		http({url:"/queryCategory",method:"get"}).then(res=>{
			tableData.value[0].children=res.data.data
			console.log(tableData.value)
		})
	}
	query()
	const dialogVisible_add = ref(false)
	const dialogVisible_update = ref(false)
	const dialogVisible_del = ref(false)
	const updateId = ref(undefined)
	const delId = ref(undefined)
	const pid = ref(undefined)
	const img = ref(undefined)
	const name = ref("")
	function add(p){
		dialogVisible_add.value = true
		pid.value = p
		console.log(p)
	}
	function addSubmit(){
		http({url:"/addCategory",method:"post",data:{pid:pid.value,name:name.value,img:img.value}}).then(res=>{
			dialogVisible_add.value = false
			query()
		})
	}
	const delName = ref("")
	function del(id,name){
		dialogVisible_del.value = true
		delId.value = id
		delName.value = name
	}
	function delSubmit(){
		http({url:"/delCategory?_id="+delId.value,method:"get"}).then(res=>{
			dialogVisible_del.value = false
			query()
		})
	}
	const updateName = ref("")
	function update(id,name,i){
		dialogVisible_update.value = true
		updateId.value = id
		updateName.value = name
		img.value = i
	}
	function updateSubmit(){
		http({url:'/updateCategory',method:"post",data:{_id:updateId.value,pid:pid.value,name:updateName.value,img:img.value}}).then(res=>{
			dialogVisible_update.value = false
			query()
		})
	}
</script>

<style>
</style>